<?php

/*

Desktopd Colors
Copyright (C) 2015  Desktopd Developer(s)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

https://www.gnu.org/licenses/agpl.html

*/


namespace Desktopd\Colors;


class xyY {
    protected $x = 0.3;
    protected $y = 0.3;
    protected $Y = 1;
    
    public static function fromXYZ(XYZ $XYZ) {
        $X = $XYZ->getX();
        $Y = $XYZ->getY();
        $Z = $XYZ->getZ();
        
        $sum = $X + $Y + $Z;
        return new self(
            $X / $sum,
            $Y / $sum,
            $Y
        );
    }
    
    public function __construct($x, $y, $Y) {
        list(
            $this->x,
            $this->y,
            $this->Y
        ) = array_map(function($c) {
            if (!is_numeric($c)) {
                throw new \InvalidArgumentException(print_r($c, true) . ' is not numeric');
            }
            
            return (float) $c;
        }, array($x, $y, $Y));
        
    }
    
    public function toXYZ() {
        $sum = $this->Y / $this->y;
        list($X, $Y, $Z) = array_map(function ($c) use($sum) {
            return $c * $sum;
        }, array(
            $this->x,
            $this->y,
            1 - $this->x - $this->y
        ));
        
        return new XYZ($X, $Y, $Z);
    }
    
    public function __toString() {
        return "xyY: ({$this->x}, {$this->y}, {$this->Y})";
    }
    
    public function get_x() {
        return $this->x;
    }
    
    public function get_y() {
        return $this->y;
    }
    
    public function getY() {
        return $this->Y;
    }
}


// vim: ts=4 et ai

